package com.cloud.other;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/7/3 9:33
 * @Desc
 * @Remarks
 */
//@Component
public class LoggerGatewayFilterFactory extends AbstractGatewayFilterFactory<LoggerGatewayFilterFactory.Config> {
    public static final String NAME_KEY = "name";
    public LoggerGatewayFilterFactory() {
        super(LoggerGatewayFilterFactory.Config.class);
    }
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(NAME_KEY);
    }
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange,
                                     GatewayFilterChain chain) {
                ServerHttpRequest request = exchange.getRequest();
                System.out.println("请求已经进来!URL:" + request.getURI());
                //TODO
                return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                    //TODO
                    System.out.println("请求准备返回了!URL:" + request.getURI());
                }));
            }
        };
    }
    public static class Config {
        private int name;
        public int getParts() {
            return name;
        }
        public void setParts(int parts) {
            this.name = parts;
        }
    }
}
